<?php
class SuppliersController extends AppController {

	var $name = 'Suppliers';

	function index() {
		$this->Supplier->recursive = 0;
		$this->set('suppliers', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid supplier', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('supplier', $this->Supplier->read(null, $id));
	}

	function add() {
        
        
		if (!empty($this->data)) {
            
            $this->Supplier->create();   
            $this->Supplier->Person->create(); 
            $supplierRole = $this->Supplier->Person->PersonRole->query("Select id from person_roles where rol = 'Proveedor'"); 
             // debug($supplierRole); 
            $person = Array  ( "Person" => Array
                                                (
                                                    "nombre" => $this->data["Supplier"]["nombre"],
                                                    "apellido" => $this->data["Supplier"]["apellido"],
                                                    "email" =>  $this->data["Supplier"]["email"],
                                                    "person_role_id" =>  $supplierRole[0]['person_roles']['id'] 
                                                )

            ) ;   
            
		
        	if($this->Supplier->Person->save($person))
            {
                $personId = $this->Supplier->Person->query("Select MAX(id) from people"); 
                
             
                $supplier =  Array  ( "Supplier" => Array
                                               (
                                                    "person_id" => $personId [0][0]['MAX(id)']
                                                )

                                                ) ; 
                
             
			    if ($this->Supplier->save($supplier)) {
				    $this->Session->setFlash(__('The supplier has been saved', true));
				    $this->redirect(array('action' => 'index'));
			    } else {
				    $this->Session->setFlash(__('The supplier could not be saved. Please, try again.', true));
			    }
            }
		}
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid supplier', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Supplier->save($this->data)) {
				$this->Session->setFlash(__('The supplier has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The supplier could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Supplier->read(null, $id);
		}
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for supplier', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Supplier->delete($id)) {
			$this->Session->setFlash(__('Supplier deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Supplier was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
?>